
package com.zut.hospital.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @program: hos-test
 * @description:
 * @author: 杨曼珍
 * @create: 2019-07-08 08:38
 **/


public class LoginInterceptor implements HandlerInterceptor {

/*
       进入Handler方法之前执行
       用于身份认证，身份授权
       比如身份认证，如果认证不通过表示当前用户没有登录，需要此方法拦截不再向下执行
     */

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        System.out.println("当前拦截URL："+request.getRequestURI());
        Object user = request.getSession().getAttribute("adminUser");
        if (user != null) {
            return true;
        }
        response.sendRedirect("/html/login.html");
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    }
}

